<?php
/**
 * Calendar Template for RoomBooking view
 */
defined('_JEXEC') or die('Restricted access');
$document =& JFactory::getDocument();
$document->addStyleSheet(JURI::base(). 'components/com_roombooking/assets/css/fullcalendar/fullcalendar.css');
$document->addScript(JURI::base(). 'components/com_roombooking/assets/js/jquery/jquery-1.5.2.min.js');
$document->addScript(JURI::base(). 'components/com_roombooking/assets/js/jquery/jquery-ui-1.8.11.custom.min.js');
$document->addScript(JURI::base(). 'components/com_roombooking/assets/js/fullcalendar/fullcalendar.min.js');
$lang = JFactory::getLanguage();
$lang = $lang->getTag();
$calendar_js="var jq = jQuery.noConflict();											
		jq(document).ready(function() {											
		var calendar = jq('#calendar').fullCalendar({								
			header: {																
				left: 'prev,next today',											
				center: 'title',													
				right: 'month,agendaWeek,agendaDay'				
			},																		
			defaultView:'agendaWeek',												
			allDaySlot:false,														
			axisFormat:'HH',														
			timeFormat:{month: 'HH:mm{-HH:mm}\\n', week: 'dd/MM HH:mm{ \\n dd/MM HH:mm}', day: 'dd/MM HH:mm{ - dd/MM HH:mm}'},								
			selectable: true, 														
			selectHelper: true,													
			minTime:'".$this->minTime."',															
			maxTime:'".$this->maxTime."',															
			weekends:".$this->show_WE.",														
			select: function(start, end) {	
				var view =jq('#calendar').fullCalendar('getView').name;
				var d = start.getDate();
				var m = start.getMonth()+1;										
				var y = start.getUTCFullYear();										
				var h = start.getHours();										
				var min = start.getMinutes();									
				if(d<10) d='0'+d;													
				if(m<10) m='0'+m;													
				if(h<10) h='0'+h;													
				if(min<10) min='0'+min;
				var startDate = y+'-'+m+'-'+d+' '+h+':'+min; 						
				d = end.getDate();
				m = end.getMonth()+1;											
				y = end.getUTCFullYear();											
				h = end.getHours();											
				min = end.getMinutes();											
				if(d<10) d='0'+d;													
				if(m<10) m='0'+m;													
				if(h<10) h='0'+h;													
				if(min<10) min='0'+min;												
				var endDate = y+'-'+m+'-'+d+' '+h+':'+min;
				var today = new Date();
				if(today <= start)						
		 			location.href='index.php?option=com_roombooking&controller=booking&task=add&start='+startDate+'&end='+endDate+'&id_room=".$this->id_room."';	
				else
					alert('".JText::_('NOBOOKINPAST')."');
		 		calendar.fullCalendar('unselect');									
			},																		
			editable: false,";  														

$file_lang = JPATH_COMPONENT.DS.'language'.DS.'fullcalendar'.DS.$lang.".php";
if (file_exists($file_lang))
	include $file_lang;
	
	
	
	
$calendar_js.="	events: [															";
foreach($this->bookings as $booking){
		$start = new DateTime($booking->start);
		$end = new DateTime($booking->end);
		$booking->short_description=str_replace("'","\'",$booking->short_description);
		$calendar_js .= "{ title: '".$booking->short_description.
		     "', start: new Date(".$start->format('Y').",".($start->format('m')-1).",".$start->format('d,H,i').")".
			 ", end: new Date(".$end->format('Y').",".($end->format('m')-1).",".$end->format('d,H,i').")".
			 ", allDay: false".
			 ", url: 'index.php?option=com_roombooking&controller=booking&cid=".$booking->id."'".
			 "},";
}

$calendar_js .=	"{}]".
		"});".
		
	"});";
$document->addScriptDeclaration($calendar_js);


?>
<form id="filter" action="index.php" name="filter">
    <div id="filter">
        <label>オーガニゼーション</label>
        <?php echo $this->organization_select;?>
        <label>ビル</label>
        <?php echo $this->building_select;?>
        <label>ルーム</label>
        <?php echo $this->room_select;?>
    </div>
    <input type="hidden" name="option" value="com_roombooking" />
    <input type="hidden" name="view" value="bookings" />
</form>
<div class="componentheading"><?php echo JText::_( 'CALENDAROF' ); if(isset($this->roomName)) echo ' '.$this->roomName; ?><?php echo ' - '.$this->getBuildOfRoom($this->id_room); ?></div>
<p><?php echo JText::_( 'INTROCAL' );?>.</p>
<div id='calendar' ></div>

<script type="text/javascript">
    jQuery(function(){
        var ob = jQuery('#calendar .fc-content .fc-view:not(.fc-button-month):visible');
        var num_tr = ob.find('.fc-agenda-body tr').size();
        ob.find('.fc-agenda-body,.fc-agenda-bg').css({'height':num_tr*21-1+'px'});

        jQuery('#calendar .fc-header-left .fc-state-default a,#calendar .fc-header-right .fc-state-default a').click(function(){
            setTimeout(function(){
                ob = jQuery('#calendar .fc-content .fc-view:not(.fc-button-month):visible');
                num_tr = ob.find('.fc-agenda-body tr').size();
                ob.find('.fc-agenda-body,.fc-agenda-bg').css({'height':num_tr*21-1+'px'});
            },0);
        });
    });
    
    var id_organization = jQuery('#id_organization');
    var id_building = jQuery('#id_building');
    var id_room = jQuery('#id_room');
    var filter = jQuery('#filter');
    id_organization.change(function(){
        id_building.val('0');
        id_room.val('0');
        filter.submit();
    });
    id_building.change(function(){
        id_room.val('0');
        filter.submit();
    });
    id_room.change(function(){
        filter.submit();
    });
    
    jQuery('#id_organization option[value=0],#id_building option[value=0],#id_room option[value=0]').hide();
</script>


